/**
 * 销售单据查询管理初始化
 */
var GoodsSaleList = {
    id: "GoodsSaleListTableId",	//表格id
    goodsSaleListData:{},
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
GoodsSaleList.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '主键', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '实付金额', field: 'amountPaid', visible: true, align: 'center', valign: 'middle'},
        {title: '销售出库订单', field: 'saleOrderNumber', visible: true, align: 'center', valign: 'middle'},
        {
            title: '是否支付',
            field: 'payState',
            visible: true,
            align: 'center',
            valign: 'middle',
            formatter: function formatter(value, row, index, field) {
                if (value === 1) {
                    return '已支付';
                } else if (value === 0) {
                    return '未支付';
                }
            }
        },
        {title: '创建者', field: 'createBy', visible: true, align: 'center', valign: 'middle'},
        {title: '创建时间', field: 'createDate', visible: true, align: 'center', valign: 'middle'},
        {title: '更新者', field: 'updateBy', visible: false, align: 'center', valign: 'middle'},
        {title: '更新时间', field: 'updateDate', visible: false, align: 'center', valign: 'middle'},
        {title: '备注信息', field: 'remarks', visible: true, align: 'center', valign: 'middle'},
        {title: '删除标记（0：正常；1：删除）', field: 'delFlag', visible: false, align: 'center', valign: 'middle'},
        {title: '客户id', field: 'customerId', visible: false, align: 'center', valign: 'middle'}
    ];
};


/**
 * 清除数据
 */
GoodsSaleList.clearData = function () {
    this.goodsSaleListData = {};
}
/**
 * 清除dom显示数据
 */
GoodsSaleList.clearDom = function () {
    this
        .setDomVal('id')
        .setDomVal('amountPaid')
        .setDomVal('saleOrderNumber')
        .setDomVal('payState')
        .setDomVal('createBy')
        .setDomVal('bDate')
        .setDomVal('updateBy')
        .setDomVal('eDate')
        .setDomVal('remarks')
        .setDomVal('delFlag')
        .setDomVal('customerId');
}

/**
 * 收集数据
 */
GoodsSaleList.collectData = function () {
    this
        .set('id')
        .set('amountPaid')
        .set('amountPayable')
        .set('saleOrderNumber')
        .set('customerId')
        .set('payState')
        .set('createBy')
        .set('createDate')
        .set('updateBy')
        .set('updateDate')
        .set('remarks')
        .set('delFlag');
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
GoodsSaleList.set = function (key, val) {
    this.goodsSaleListData[key] = (typeof val === "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置dom中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
GoodsSaleList.setDomVal = function (key, val) {
    (typeof val === "undefined") ? $("#" + key).val('') : $("#" + key).val(val);
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
GoodsSaleList.get = function (key) {
    return $("#" + key).val();
}


/**
 * 检查是否选中
 */
GoodsSaleList.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length === 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        GoodsSaleList.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加销售单据查询
 */
GoodsSaleList.openAddGoodsSaleList = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '销售单据查询',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/goodsSaleList/goodsSaleList_detail/' + GoodsSaleList.seItem.id + '/' + GoodsSaleList.seItem.saleOrderNumber
        });
        this.layerIndex = index;
    }
};

/**
 * 打开查看销售单据查询详情
 */
GoodsSaleList.openGoodsSaleListDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '销售单据查询详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/goodsSaleList/goodsSaleList_update/' + GoodsSaleList.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除销售单据查询
 */
GoodsSaleList.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/goodsSale/delete", function (data) {
            Feng.success("删除成功!");
            GoodsSaleList.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("goodsSaleListId", this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询销售单据查询列表
 */
GoodsSaleList.search = function () {
    var queryData = {};
    queryData['saleOrderNumber'] = $("#saleOrderNumber").val();
    queryData['payState'] = $("#payState").val();
    queryData['remarks'] = $("#remarks").val();
    queryData['bDate'] = $("#bDate").val();
    queryData['eDate'] = $("#eDate").val();
    console.log('查找条件：', JSON.stringify(queryData));

    //刷新表格并带上分页
    // PurchaseGoods.purchaseListTable.refresh(
    //     {
    //         pageNumber: PurchaseGoods.purchaseListTable.getPageNumber(),
    //         pageSize: PurchaseGoods.purchaseListTable.getPageSize(),
    //         url: "/purchase/list",
    //         query: queryData
    //     });
    GoodsSaleList.table.refresh({query: queryData});
};

/**
 * 清空查询数据
 */
GoodsSaleList.clear = function(){

}


/**
 * 是否支付下拉框
 */
GoodsSaleList.isPaySelect = function () {
    $("#payState").html("<option value='1'>已支付</option>" +
        "<option value='0'>未支付</option>")
}

$(function () {
    var defaultColumns = GoodsSaleList.initColumn();
    var table = new BSTable(GoodsSaleList.id, "/goodsSale/list", defaultColumns);
    table.setTableHeight(0);
    table.setShowColumns(false);
    table.setShowRefresh(false);
    GoodsSaleList.table = table.init();
    //是否支付
    GoodsSaleList.isPaySelect();
});
